package com.jike.jkgame;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.jike.jkgame.data.AttackResultData;
import com.jike.jkgame.data.Player;

public class AttackResultDialog extends DialogFragment implements OnClickListener {
	
	private OnAttackWinListner mListner;
	private AttackResultData mData;
	
	public static AttackResultDialog newInstance(OnAttackWinListner listner, AttackResultData data) {
		AttackResultDialog dialog = new AttackResultDialog(listner);
		Bundle args = new Bundle();
		args.putSerializable("data", data);
		dialog.setArguments(args);
		return dialog;
	}
	
	public AttackResultDialog(OnAttackWinListner listner) {
		super();
		mListner = listner;
	}
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		getDialog().requestWindowFeature(STYLE_NO_TITLE);
		
		Bundle args = getArguments();
		if (args != null) {
			mData = (AttackResultData) args.getSerializable("data");
		}
		
		if (mData == null) {
			dismiss();
			return null;
		}
		
		View v = inflater.inflate(R.layout.attack_result_dialog_layout, container);
		Rect displayRectangle = new Rect();
		Window window = getDialog().getWindow();
		window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
		v.setMinimumWidth((int) (displayRectangle.width() * 0.7f));
		v.setMinimumHeight((int) (displayRectangle.height() * 0.9f));
		
		TextView titleTv = (TextView) v.findViewById(R.id.tv_title);
		titleTv.setText(Utilis.getResString(mData.result == 1 ? R.string.attack_win : R.string.attack_fail));
		
		if (mData.equip > 0) {
			ImageView iv = (ImageView) v.findViewById(R.id.iv_props);
			iv.setVisibility(View.VISIBLE);
			
			TextView tv = (TextView) v.findViewById(R.id.tv_props_info);
			tv.setVisibility(View.VISIBLE);
		}
		
		TextView infoTv = (TextView) v.findViewById(R.id.tv_rewards);
		infoTv.setText(mData.getFormatedString());

		v.findViewById(R.id.btn_compare).setOnClickListener(this);
		v.findViewById(R.id.btn_ok).setOnClickListener(this);
		
		return v;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_compare:
			mListner.compare(mData.player);
			dismiss();
			break;
			
		case R.id.btn_ok:
			dismiss();
			break;

		default:
			break;
		}
		
	}
	
	public interface OnAttackWinListner {
		public void compare(Player player);
	}

}
